package com.gome.gop.common;

import java.io.IOException;
import java.io.Serializable;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * @see 数据返回对象 RestData.java gome.com.cn
 * @copyright 2018年2月5日 下午2:39:19
 * @author jiangdabin
 */
/** 
* @ClassName: RestData 
* @Description: TODO
* @author  chengcheng 1294480405@qq.com
* @date 2018年4月18日 下午3:38:07  
*/
public class RestData implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 状态码
	 */
	private String code;
	/**
	 * 数据结果
	 */
	private Object data;
	/**
	 * 操作是否成功
	 */
	private String success;
	/**
	 * 消息
	 */
	private String msg;

	/* 异常信息 */
	private String exceptionMsg;

	/**
	 * 返回默认成功对象
	 * 
	 * @param data
	 *            返回的数据
	 * @return
	 */
	public static RestData makeSuccess(Object data) {
		RestData result = new RestData();
		result.setSuccess(StatusCode.SUCCESS_TRUE);
		result.setCode(StatusCode.CODE_SUCCESS);
		result.setMsg(StatusCode.SUCCESS_MSG);
		result.setData(data);
		return result;
	}

	/**
	 * <b>设置信息</b>
	 * <p>
	 * </p>
	 * 
	 * @param msg
	 *            返回的msg ,非data
	 * @return
	 */
	public static RestData makeSuccess(String msg) {
		RestData result = new RestData();
		result.setSuccess(StatusCode.SUCCESS_TRUE);
		result.setCode(StatusCode.CODE_SUCCESS);
		result.setMsg(msg);
		return result;
	}

	/**
	 * <b></b>
	 * <p>
	 * </p>
	 * 
	 * @param data
	 * @param isData
	 *            内容是否为的内容
	 * @return
	 */
	public static RestData makeSuccess(String data, boolean isData) {
		RestData result = new RestData();
		result.setSuccess(StatusCode.SUCCESS_TRUE);
		result.setCode(StatusCode.CODE_SUCCESS);
		result.setMsg(StatusCode.SUCCESS_MSG);
		result.setData(data);
		return result;
	}

	public static RestData makeSuccess() {
		RestData result = new RestData();
		result.setSuccess(StatusCode.SUCCESS_TRUE);
		result.setCode(StatusCode.CODE_SUCCESS);
		result.setMsg(StatusCode.SUCCESS_MSG);
		return result;
	}

	/**
	 * 返回默认的对象
	 * 
	 * @param data
	 *            要返回的参数
	 * @param code
	 *            状态码
	 * @return
	 */
	public static RestData makeSuccessCode(Object data, String code) {
		RestData result = new RestData();
		result.setSuccess(StatusCode.SUCCESS_TRUE);
		result.setCode(code);
		result.setMsg(StatusCode.SUCCESS_MSG);
		result.setData(data);
		return result;
	}

	/**
	 * 返回成功的对象
	 * 
	 * @param data
	 *            要返回的参数
	 * @param msg
	 *            提示信息
	 * @return
	 */
	public static RestData makeSuccess(Object data, String msg) {
		RestData result = new RestData();
		result.setSuccess(StatusCode.SUCCESS_TRUE);
		result.setCode(StatusCode.CODE_SUCCESS);
		result.setData(data);
		result.setMsg(msg);
		return result;
	}

	/**
	 * 返回成功的对象
	 * 
	 * @param data
	 *            要返回的参数
	 * @param code
	 *            状态码
	 * @param msg
	 *            提示信息
	 * @return
	 */
	public static RestData makeSuccess(Object data, String code, String msg) {
		RestData result = new RestData();
		result.setSuccess(StatusCode.SUCCESS_TRUE);
		result.setCode(code);
		result.setData(data);
		result.setMsg(msg);
		return result;
	}

	/**
	 * 返回失败的默认对象
	 * 
	 * @param data
	 *            返回的数据
	 * @return
	 */
	public static RestData makeError(Object data) {
		RestData result = new RestData();
		result.setSuccess(StatusCode.SUCCESS_FALSE);
		result.setCode(StatusCode.CODE_ERROR_DEFAULT);
		result.setMsg(StatusCode.ERROR_MSG);
		result.setData(data);
		return result;
	}

	public static RestData makeError(String msg) {
		RestData result = new RestData();
		result.setSuccess(StatusCode.SUCCESS_FALSE);
		result.setCode(StatusCode.CODE_ERROR_DEFAULT);
		result.setMsg(msg);
		return result;
	}

	public static RestData makeError() {
		RestData result = new RestData();
		result.setSuccess(StatusCode.SUCCESS_FALSE);
		result.setCode(StatusCode.CODE_ERROR_DEFAULT);
		result.setMsg(StatusCode.ERROR_MSG);
		return result;
	}

	/**
	 * 返回失败的默认的对象
	 * 
	 * @param data
	 *            要返回的参数
	 * @param code
	 *            状态码
	 * @return
	 */
	public static RestData makeErrorCode(Object data, String code) {
		RestData result = new RestData();
		result.setSuccess(StatusCode.SUCCESS_FALSE);
		result.setCode(code);
		result.setMsg(StatusCode.ERROR_MSG);
		result.setData(data);
		return result;
	}

	/**
	 * 返回失败的默认的对象
	 * 
	 * @param data
	 *            要返回的参数
	 * @param msg
	 *            提示信息
	 * @return
	 */
	public static RestData makeError(Object data, String msg) {
		RestData result = new RestData();
		result.setSuccess(StatusCode.SUCCESS_FALSE);
		result.setCode(StatusCode.CODE_ERROR_DEFAULT);
		result.setData(data);
		result.setMsg(msg);
		return result;
	}

	/**
	 * 返回失败的默认对象
	 * 
	 * @param data
	 *            要返回的参数
	 * @param code
	 *            状态码
	 * @param msg
	 *            提示信息
	 * @return
	 */
	public static RestData makeError(Object data, String code, String msg) {
		RestData result = new RestData();
		result.setSuccess(StatusCode.SUCCESS_FALSE);
		result.setCode(code);
		result.setData(data);
		result.setMsg(msg);
		return result;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public String getSuccess() {
		return success;
	}

	public void setSuccess(String success) {
		this.success = success;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public static void main(String args[]) throws JsonGenerationException, JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		mapper.convertValue(RestData.makeSuccess(), Map.class);
	}

	public String getExceptionMsg() {
		return exceptionMsg;
	}

	public void setExceptionMsg(String exceptionMsg) {
		this.exceptionMsg = exceptionMsg;
	}

    @Override
    public String toString() {
        return "RestData [code=" + code + ", data=" + data + ", success=" + success + ", msg=" + msg + ", exceptionMsg=" + exceptionMsg + "]";
    }

}
